import { createContext, useContext, useState, useRef } from 'react'
import { OrbitControls } from '@react-three/drei'
import { useLightStore } from '@/stores'

interface InteractionContextType {
  isDragging: boolean
  setIsDragging: (v: boolean) => void
  controlsRef: React.RefObject<any>
}

const InteractionContext = createContext<InteractionContextType>({
  isDragging: false,
  setIsDragging: () => {},
  controlsRef: { current: null },
})

export function useInteraction() {
  return useContext(InteractionContext)
}

export function Scene() {
  const [isDragging, setIsDragging] = useState(false)
  const controlsRef = useRef<any>(null)
  const lightConfig = useLightStore((state) => state.lightConfig)
  console.log('🚀 ~ lightConfig:', lightConfig)

  return (
    <InteractionContext.Provider value={{ isDragging, setIsDragging, controlsRef }}>
      <ambientLight intensity={lightConfig.ambientIntensity} />
      <directionalLight position={lightConfig.directionalPosition} intensity={lightConfig.directionalIntensity} />
      <OrbitControls
        ref={controlsRef}
        enabled={!isDragging}
        enableDamping
        dampingFactor={0.05}
        minDistance={1}
        maxDistance={50}
      />
    </InteractionContext.Provider>
  )
}
